/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Relatorios.java
 *
 * Created on 21/09/2009, 10:43:09
 */

package JPaneis;

import siii.MenuPadrao;
import Conector.Conector;
import java.awt.Component;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;


public class Relatorios extends javax.swing.JPanel {

    private MenuPadrao menuPadrao;
    private String relatorioDesejado;
    private String dataInicio;
    private String dataFim;
    private Conector conector;
    /** Creates new form Relatorios */
    public Relatorios(MenuPadrao m) {
        menuPadrao = m;
        initComponents();
    }

    public Relatorios(MenuPadrao m, Conector con) {
        menuPadrao = m;
        this.conector = con;
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelTermino = new javax.swing.JLabel();
        jLabelInicio = new javax.swing.JLabel();
        relDesejado = new javax.swing.JComboBox();
        jLabelRelatorio = new javax.swing.JLabel();
        jLabelRelatórioDesejado = new javax.swing.JLabel();
        jButtonCancelar = new javax.swing.JButton();
        jButtonVisualizar = new javax.swing.JButton();
        relDataInicio = new javax.swing.JFormattedTextField();
        relDataTermino = new javax.swing.JFormattedTextField();
        jScrollPaneServPesc = new javax.swing.JScrollPane();
        jListRelatorio = new javax.swing.JList();
        jLabelValorTotal = new javax.swing.JLabel();
        jLabelReal = new javax.swing.JLabel();
        jLabelValor = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();

        jLabelTermino.setText("Data de Término");

        jLabelInicio.setText("Data de Início");

        relDesejado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Balanço", "Compra de Peixe Piscina", "Compra de Peixe Tanque", "Pescadores no Tanque", "Serviços Pesca", "Serviços Piscina" }));
        relDesejado.setNextFocusableComponent(jButtonVisualizar);

        jLabelRelatorio.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabelRelatorio.setText("Relatórios");

        jLabelRelatórioDesejado.setText("Relatório Desejado");

        jButtonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/peixe_cancelar.png"))); // NOI18N
        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonCancelarMouseClicked(evt);
            }
        });

        jButtonVisualizar.setText("Visualizar");
        jButtonVisualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVisualizarActionPerformed(evt);
            }
        });

        try {
            relDataInicio.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        relDataInicio.setToolTipText("Digite o CPF");

        try {
            relDataTermino.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        relDataTermino.setToolTipText("Digite o CPF");

        jListRelatorio.setName("Lista de Cadastro de Setviço de Pesca"); // NOI18N
        jScrollPaneServPesc.setViewportView(jListRelatorio);

        jLabelValorTotal.setText("Valor Total:");

        jLabelReal.setText("R$");

        jLabelValor.setText("00.00");

        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/relatorios.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelRelatorio)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabelRelatórioDesejado)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(relDesejado, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelTermino)
                                    .addComponent(jLabelInicio))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(relDataInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(relDataTermino, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButtonVisualizar)))))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabelValorTotal)
                                    .addGap(39, 39, 39)
                                    .addComponent(jLabelReal)))
                            .addGap(26, 26, 26)
                            .addComponent(jLabelValor)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(jButtonCancelar)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPaneServPesc, javax.swing.GroupLayout.PREFERRED_SIZE, 653, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPaneServPesc, javax.swing.GroupLayout.PREFERRED_SIZE, 454, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelRelatorio, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelRelatórioDesejado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(relDesejado))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelInicio)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabelTermino))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(relDataInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(relDataTermino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButtonVisualizar))
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelValorTotal)
                            .addComponent(jLabelValor)
                            .addComponent(jLabelReal))
                        .addGap(31, 31, 31)
                        .addComponent(jLabel13)
                        .addGap(30, 30, 30)
                        .addComponent(jButtonCancelar)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

//GEN-FIRST:event_pescCPF1ActionPerformed

//GEN-LAST:event_pescCPF1ActionPerformed

    private void jButtonVisualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVisualizarActionPerformed
         String[][] dadosRelatorio = null;
         float totaliza = 0;
        try {
            int itemRelatorio = relDesejado.getSelectedIndex();
            dataInicio = relDataInicio.getText();
            dataFim = relDataTermino.getText();


            //ServicoPesca servPesca = this.readScreen();
            if(itemRelatorio ==  0) {//'Balanço') {
                jLabelValorTotal.setVisible(true);
                jLabelReal.setVisible(true);
                jLabelValor.setVisible(true);
                dadosRelatorio = conector.balanco(dataInicio, dataFim);

            }
            if(itemRelatorio == 1){ //'Compra de Peixe Piscina')
                jLabelValorTotal.setVisible(true);
                jLabelReal.setVisible(true);
                jLabelValor.setVisible(true);
                dadosRelatorio = conector.comprasPiscina(dataInicio, dataFim);
                for (int i=1; i < dadosRelatorio.length; i++)
                    totaliza+= Float.parseFloat(dadosRelatorio[i][2]);
                jLabelValor.setText(Float.toString(totaliza));
            }
            if(itemRelatorio == 2){ //'Compra de Peixe Tanque')
                jLabelValorTotal.setVisible(true);
                jLabelReal.setVisible(true);
                jLabelValor.setVisible(true);
                dadosRelatorio = conector.comprasTanque(dataInicio, dataFim);
              //  List<Map<String,?>> dadosTanque =  conector.doRawSql("SELECT SUM(VALOR) AS VALOR FROM TANQUE");
              //  Float total = (Float) dadosTanque.get(0).get("VALOR");
              //  jLabelValor.setText(Float.toString(total));
                for (int i=1; i < dadosRelatorio.length; i++)
                    totaliza+= Float.parseFloat(dadosRelatorio[i][2]);
                jLabelValor.setText(Float.toString(totaliza));
            }
            if(itemRelatorio == 3){ //'Pescadores no Tanque')
                jLabelValorTotal.setVisible(false);
                jLabelReal.setVisible(false);
                jLabelValor.setVisible(false);
                dadosRelatorio = conector.pescadoresTanque(dataInicio, dataFim);
                
            }
            if(itemRelatorio == 4){ //"Serviços Pesca")
                jLabelValorTotal.setVisible(true);
                jLabelReal.setVisible(true);
                jLabelValor.setVisible(true);
                dadosRelatorio = conector.relatServicosPesca(dataInicio, dataFim);
                for (int i=1; i < dadosRelatorio.length; i++)
                    totaliza+= Float.parseFloat(dadosRelatorio[i][4]);
                jLabelValor.setText(Float.toString(totaliza));
            }
            if(itemRelatorio == 5){ //"Serviços Piscina")
                jLabelValorTotal.setVisible(true);
                jLabelReal.setVisible(true);
                jLabelValor.setVisible(true);
                dadosRelatorio = conector.relatServicosPiscina(dataInicio, dataFim);
                for (int i=1; i < dadosRelatorio.length; i++)
                    totaliza+= Float.parseFloat(dadosRelatorio[i][4]);
                jLabelValor.setText(Float.toString(totaliza));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if(dadosRelatorio != null) {
          jListRelatorio.setListData(dadosRelatorio);
          //create Renderer and dislpay
          System.out.println("Total: " + totaliza);
          jListRelatorio.setCellRenderer((ListCellRenderer) new MyCellRenderer(dadosRelatorio[0].length));
        }
    }//GEN-LAST:event_jButtonVisualizarActionPerformed

    private void jButtonCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonCancelarMouseClicked
        // TODO add your handling code here:
        this.menuPadrao.trocaPanel2(menuPadrao.getMenuPrincipal());
    }//GEN-LAST:event_jButtonCancelarMouseClicked


static class MyCellRenderer extends JPanel implements ListCellRenderer {

        ArrayList<JLabel> labels = new ArrayList();
        JLabel left, middle, right;
        int cols;

        MyCellRenderer(int cols) {
            this.cols = cols;
            setLayout(new GridLayout(1, cols));
            for (int i=0; i < cols;i++){
                JLabel label = new JLabel();
                label.setOpaque(true);
                add(label);
                labels.add(label);
            }
        }

        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            int i = 0;
            for (JLabel jl : labels) {
                jl.setText(((String[])value)[i++]) ;
                if (isSelected) {
                    jl.setBackground(list.getSelectionBackground());
                    jl.setForeground(list.getSelectionForeground());
                } else {
                    jl.setBackground(list.getBackground());
                    jl.setForeground(list.getForeground());
                }
            }

            setEnabled(list.isEnabled());
            setFont(list.getFont());
            return this;
        }
    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonVisualizar;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabelInicio;
    private javax.swing.JLabel jLabelReal;
    private javax.swing.JLabel jLabelRelatorio;
    private javax.swing.JLabel jLabelRelatórioDesejado;
    private javax.swing.JLabel jLabelTermino;
    private javax.swing.JLabel jLabelValor;
    private javax.swing.JLabel jLabelValorTotal;
    private javax.swing.JList jListRelatorio;
    private javax.swing.JScrollPane jScrollPaneServPesc;
    private javax.swing.JFormattedTextField relDataInicio;
    private javax.swing.JFormattedTextField relDataTermino;
    private javax.swing.JComboBox relDesejado;
    // End of variables declaration//GEN-END:variables

}
